package com.yuanfeng.business.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yuanfeng.business.dto.NewAgreementStatusUpdatedDTO;
import com.yuanfeng.business.dto.UserTokenBeanDTO;
import com.yuanfeng.business.entity.ShopAgreementEntity;
import com.yuanfeng.business.mapper.ShopAgreementMapper;
import com.yuanfeng.business.service.ShopAgreementService;
import com.yuanfeng.commoms.exception.BizCodeEnume;
import com.yuanfeng.commoms.util.ParseTokenUtils;
import com.yuanfeng.commoms.util.ResponseResult;
import com.yuanfeng.commoms.vo.user.UserInfoVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
 * @ClassName ShopAgreementServiceImpl
 * @Description 店铺协议
 * @Author cf
 * @Date 2023/6/26 9:35
 * @Version 1.0
 */
@Service
public class ShopAgreementServiceImpl extends ServiceImpl<ShopAgreementMapper,ShopAgreementEntity> implements ShopAgreementService {

    @Autowired
    private ShopAgreementMapper shopAgreementMapper;

    @Override
    public ResponseResult checkIsHaveAgreement(UserTokenBeanDTO param) {
        // 不弹
        Boolean flag = false;

        UserInfoVO userInfo = ParseTokenUtils.getUserInfoByToken(param.getToken());

        Integer agreementId = shopAgreementMapper.selectNewAgreement();
        if (null == agreementId) {
            return ResponseResult.success(flag);
        }
        // shopIsAgree 店铺是否同意(0-不同意,1-同意,2-失效)
        Integer shopIsAgree = shopAgreementMapper.selectShopAgreement(Integer.valueOf(userInfo.getShopId()), agreementId);

        if (null == shopIsAgree || shopIsAgree.intValue() == 0 || shopIsAgree.intValue() == 2) {
            flag = true;
        }

        return ResponseResult.success(flag);
    }

    @Override
    public ResponseResult updateNewAgreementStatus(NewAgreementStatusUpdatedDTO param) {
        UserInfoVO userInfo = ParseTokenUtils.getUserInfoByToken(param.getToken());
        param.setShopId(Integer.valueOf(userInfo.getShopId()));
        param.setShopIsAgree(1);
        param.setUpdateDate(new Date());

        this.baseMapper.insertNewAgreementStatus(param);

        return ResponseResult.success(BizCodeEnume.OPERATION_SUCCESSFUL);
    }

}
